package cn.yqx.bankshop.remote;


import cn.yqx.bankshop.util.MD5Util;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class SmsSendRemote {

    private static final String KX_VOICE_URL = "http://47.99.226.235:8081/api/sms/send" ;
    private static final String KX_VOICE_USERNAME = "661254" ;
    private static final String KX_VOICE_APIKEY= "bec5380359e84a329053987ef52453e1";

    public JSONObject yqxSmsSend(String url , String userid,String sign, String phone, String msgContent){
        Map<String, Object> params = new HashMap<String, Object>(10);
        params.put("account", userid);
        params.put("mobile", phone);
        params.put("text", msgContent);
        params.put("sign", sign);
        params.put("extend", "");

        try {
            String result = RequestUtil.net(url, params, "POST");

            return JSONObject.parseObject(result);

        } catch (Exception e) {
           log.info("SmsSendRemote is error : {} ",e);
        }

        JSONObject json = new JSONObject() ;
        json.put("errcode",5000) ;
        json.put("errmsg","Network failure") ;


        return  json ;
    }

    public JSONObject yqxSmsSendvoice(String url,String userid, String apikey , String phone, String msgContent){

        byte[]  bytes = (userid + apikey).getBytes();
        byte[] signBytes = Base64.getEncoder().encode((DigestUtils.md5DigestAsHex(bytes)).getBytes());
        String sign = new String(signBytes) ;
        Map<String, Object> params = new HashMap<String, Object>(10);
        params.put("account", userid);
        params.put("mobile", phone);
        params.put("text", msgContent);
        params.put("sign", sign);
        params.put("extend", "");

        try {
            String result = RequestUtil.net(url, params, "POST");
            return JSONObject.parseObject(result);

        } catch (Exception e) {
            log.info("SmsSendRemote is error : {} ",e);
        }

        JSONObject json = new JSONObject() ;
        json.put("errcode",5000) ;
        json.put("errmsg","Network failure") ;
        return  json ;
    }

    public static String kxSmsvoice(String mobile, String msgcontent) {
        String ts = System.currentTimeMillis() + "";
        String sign = MD5Util.MD5(KX_VOICE_USERNAME + ts + KX_VOICE_APIKEY);
        Map<String, Object> params = new HashMap<String, Object>(10);
        params.put("userid", KX_VOICE_USERNAME);
        params.put("ts", ts);
        params.put("apikey", KX_VOICE_APIKEY);
        params.put("sign", sign.toLowerCase());
        params.put("mobile", mobile);
        params.put("msgcontent", msgcontent);
        params.put("extnum", "");

        String result = null;
        try {
            result = RequestUtil.netkx(KX_VOICE_URL, params, "POST");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result ;
    }

    public static void main(String[] args) throws Exception {
        SmsSendRemote SmsSendRemote = new SmsSendRemote() ;

        String txt = SmsSendRemote.kxSmsvoice("15067131818","平台有未发的数据，请尽快查看");

    }
}
